﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client.Tasks;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Eaglight
{
    public class IdentifyResultsPanel: ContentControl
    {
        private ObservableCollection<IdentifyResult> identifyResults;
        ComboBox cmbLayers;
        TextBlock lblInfo;
        DataGrid dgAttributes;
        Button btnClose;

        public IdentifyResultsPanel()
        {
            DefaultStyleKey = typeof(IdentifyResultsPanel);
            identifyResults = new ObservableCollection<IdentifyResult>();
        }

        private void initUI()
        {
            cmbLayers = (ComboBox)GetTemplateChild("cmbLayers");
            cmbLayers.ItemsSource = identifyResults;
            lblInfo = (TextBlock)GetTemplateChild("lblInfo");
            dgAttributes = (DataGrid)GetTemplateChild("dgAttributes");
            btnClose = (Button)GetTemplateChild("btnClose");
            cmbLayers.SelectionChanged += new SelectionChangedEventHandler(cmbLayers_SelectionChanged);
            btnClose.Click += new RoutedEventHandler(btnClose_Click);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            initUI();
        }

        void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

        void cmbLayers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IdentifyResult result = cmbLayers.SelectedItem as IdentifyResult;
            if (result != null)
            {
                dgAttributes.ItemsSource = result.Feature.Attributes;
            }
        }

        protected static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(IdentifyResultsPanel), null);

        public CornerRadius CornerRadius
        {
            get
            {
                return (CornerRadius)GetValue(CornerRadiusProperty);
            }
            set
            {
                SetValue(CornerRadiusProperty, value);
            }
        }

        public IList<IdentifyResult> IdentifyResults
        {
            get { return this.identifyResults; }
            set
            {
                if (identifyResults != null)
                    identifyResults.Clear();
                foreach (IdentifyResult r in value)
                {
                    identifyResults.Add(r);
                }
                cmbLayers.SelectedIndex = 0;
            }
        }

    }
}
